package com.liangsui.cmall.member.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * CmallFeignConfig
 *
 * @author zhangjiawei
 * @email 1533167396@qq.com
 * @date 2022/8/19 17:11
 */
@Configuration
public class CmallFeignConfig {

    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (requestAttributes != null) {
                    HttpServletRequest request = requestAttributes.getRequest();
                    //同步老请求头信息
                    if (request != null) {
                        requestTemplate.header("Cookie", request.getHeader("Cookie"));
                    }
                }
            }
        };
    }
}
